package com.cqut.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cqut.model.SignUpManager;
import com.cqut.service.ISignUpManagerService;
import com.cqut.util.SqlUtil;
import com.cqut.dao.SignUpManagerMapper;
import com.cqut.dao.SignUpManagerMapperExtra;
import com.cqut.dto.LimitShowDTO;
import com.cqut.dto.SignUpManagerDTO;
import com.cqut.dto.TeamMemberDTO;
import com.cqut.dto.UserDTO;

@Service
public class SignUpManagerService implements ISignUpManagerService {
	@Resource(name="signUpManagerMapper")
    private SignUpManagerMapper signUpManagerMapper;

	@Resource(name="signUpManagerMapperExtra")
    private SignUpManagerMapperExtra signUpManagerMapperExtra;
	
    @Override
    public boolean save(SignUpManager signUpManager) {
        return signUpManagerMapper.insert(signUpManager) > 0;
    }

    @Override
    public boolean update(SignUpManager signUpManager) {
        return signUpManagerMapper.updateByPrimaryKeySelective(signUpManager) >= 0;
    }

    @Override
    public boolean delete(String signUpId) {
        return signUpManagerMapper.deleteByPrimaryKey(signUpId) > 0;
    }

    @Override
    public boolean batchDelete(String[] primaryKeys) {
        return signUpManagerMapper.deleteByPrimaryKeys(primaryKeys) > 0;
    }

    @Override
    public SignUpManager findModel(String signUpId) {
        return signUpManagerMapper.selectByPrimaryKey(signUpId);
    }

    @Override
    public SignUpManager findModel(String signUpId, String[] parameters) {
        return signUpManagerMapper.findModel(signUpId, SqlUtil.formatParameters(parameters));
    }

    public Map<String, Object> findMap(String signUpId) {
        String[][] parameters = {{"sign_up_id", "signUpId"}, {"sign_up_type", "signUpType"}, {"team_name", "teamName"}, {"competition_title", "competitionTitle"}, {"sign_up_time", "signUpTime"}, {"state", "state"}};
        return this.findMap(signUpId, parameters);
    }

    public Map<String, Object> findMap(String signUpId, String[][] parameters) {
        return signUpManagerMapper.findMap(signUpId, SqlUtil.formatParametersForAlias(parameters));
    }

    @Override
    public List<SignUpManager> loadModels() {
        return this.loadModels(null, null, null, null, -1, -1);
    }

    @Override
    public List<SignUpManager> loadModels(String condition, Object[] values, String order, String sort, int curPage, int limit) {
        return this.loadModels(null, condition, values, order, sort, curPage, limit);
    }

    @Override
    public List<SignUpManager> loadModels(String[] parameters, String condition, Object[] values, String order, String sort, int curPage, int limit) {
        return signUpManagerMapper.loadModels(SqlUtil.formatParameters(parameters), SqlUtil.fillCondition(condition, values), order, sort, SqlUtil.getOffset(curPage, limit), limit);
    }

    @Override
    public List<Map<String, Object>> loadMaps() {
        String[][] parameters = {{"sign_up_id", "signUpId"}, {"sign_up_type", "signUpType"}, {"team_name", "teamName"}, {"competition_title", "competitionTitle"}, {"sign_up_time", "signUpTime"}, {"state", "state"}};
        return this.loadMaps(parameters, null, null, null, null, -1, -1);
    }

    @Override
    public List<Map<String, Object>> loadMaps(String condition, Object[] values, String order, String sort, int curPage, int limit) {
        String[][] parameters = {{"sign_up_id", "signUpId"}, {"sign_up_type", "signUpType"}, {"team_name", "teamName"}, {"competition_title", "competitionTitle"}, {"sign_up_time", "signUpTime"}, {"state", "state"}};
        return this.loadMaps(parameters, condition, values, order, sort, curPage, limit);
    }

    @Override
    public List<Map<String, Object>> loadMaps(String[][] parameters, String condition, Object[] values, String order, String sort, int curPage, int limit) {
        return signUpManagerMapper.loadMaps(SqlUtil.formatParametersForAlias(parameters), SqlUtil.fillCondition(condition, values), order, sort, SqlUtil.getOffset(curPage, limit), limit);
    }

    @Override
    public long count() {
        return this.count(null, false);
    }

    @Override
    public long count(String condition, Object[] values) {
        return this.count(null, condition, values, false);
    }

    @Override
    public long count(String[] parameters, boolean isDistinct) {
        return this.count(parameters, null, null, isDistinct);
    }

    @Override
    public long count(String[] parameters, String condition, Object[] values, boolean isDistinct) {
        return signUpManagerMapper.count(SqlUtil.formatParameters(parameters), SqlUtil.fillCondition(condition, values), isDistinct);
    }
    
    /**
     * 根据条件获得总列表
     * @param competeName 竞赛名称
     * @param stateName 阶段名称
     * @param state 状态
     * **/
	@Override
	public List<Map<String, Object>> findSignUpListWithLimit(String competeName, String stateName, String state,
			LimitShowDTO limitShowDTO) {
		return signUpManagerMapperExtra.findSignUpListWithLimit(competeName, stateName, state, limitShowDTO.calculateStart(), limitShowDTO.calculateEnd());
	}

	/**
	 * 根据条件获得条数
     * @param competeName 竞赛名称
     * @param stateName 阶段名称
     * @param state 状态
     * **/
	@Override
	public long findCountOfSignUpList(String competeName, String stateName, String state) {
		return signUpManagerMapperExtra.findCountByLimit(competeName, stateName, state);
	}

	/**
	 * 审核通过
	 * @param passIds
	 * **/
	@Override
	public boolean passSignUpMenbers(String[] passIds) {
		return (signUpManagerMapperExtra.passSignUpMens(passIds)>0);
	}

	/**
	 * 审核退回
	 * @param passIds
	 * **/
	@Override
	public boolean backSignUpMenbers(String[] backIds) {
		return (signUpManagerMapperExtra.backSignUpMens(backIds)>0);
	}
    
	/**
	 * 报名者详细信息	
	 * @param signUpId
	 * */
	@Override
	public Map<String, Object> signUpIdDetail(String signUpId) {		
		return signUpManagerMapperExtra.findSignUpIdDetail(signUpId);
	}

	/**
	 * 得到阶段名称
	 * 
	 * */
	@Override
	public List<Map<String, Object>> getStateName() {
		return signUpManagerMapperExtra.findStateName();
	}

	/**
	 * 得到状态
	 * */
	@Override
	public List<Map<String, Object>> getState() {
		return signUpManagerMapperExtra.findState();
	}

	/**
	 * 得到老师信息
	 * */
	@Override
	public List<Map<String, Object>> signUpIdTeacher(String signUpId) {
		return signUpManagerMapperExtra.findTeacherDetail(signUpId);
	}

	/**
	 * 得到负责人信息
	 * */
	@Override
	public List<Map<String, Object>> signUpIdMain(String signUpId) {
		return signUpManagerMapperExtra.findMainDetail(signUpId);
	}

	/**
	 * 得到成员信息
	 * */
	@Override
	public List<Map<String, Object>> signUpIdMenber(String signUpId) {
		return signUpManagerMapperExtra.findMenberDetail(signUpId);
	}
	
	/**
	 * 组长新增
	 * @param problemManager
	 * @return
	 */
	@Override
    public String menberAdd(TeamMemberDTO teamMemberDTO) {
		String teamMember = String.valueOf(System.currentTimeMillis());//设置团队Id
		teamMemberDTO.setTeamMember(teamMember);
		signUpManagerMapperExtra.addTeam(teamMemberDTO);
    	return teamMember;     
    }
	
	/**
	 * 成员新增
	 * @param problemManager
	 * @return
	 */
	@Override
    public String grouperAdd(TeamMemberDTO teamMemberDTO) {
		String teamMember = String.valueOf(System.currentTimeMillis());//设置团队Id
		teamMemberDTO.setTeamMember(teamMember);
		signUpManagerMapperExtra.addGrouper(teamMemberDTO);
    	return teamMember;    
    }
	
	/**
	 * 报名信息新增
	 * @param problemManager
	 * @return
	 */
	@Override
    public String applyAdd(SignUpManagerDTO signUpManagerDTO) {
    	String signUpId = String.valueOf(System.currentTimeMillis());
    	signUpManagerDTO.setSignUpId(signUpId);
    	signUpManagerMapperExtra.addApplication(signUpManagerDTO);
    	
    	return signUpId;
	}
	
	/**
     * 根据条件获得总列表
	 *	@param teamMember 团队ID
     * **/
	@Override
	public List<Map<String, Object>> getMemberList(String signUpId, 
			LimitShowDTO limitShowDTO) {
		return signUpManagerMapperExtra.findMemberList(signUpId, limitShowDTO.calculateStart(), limitShowDTO.calculateEnd());
	}
	
	/**
	 * 获得成员条数
     * @param teamMember 团队ID
     * **/
	@Override
	public long getMemberListCount(String signUpId) {
		return signUpManagerMapperExtra.findMemberCount(signUpId);
	}
}
